<?php
/**
 * 车辆档案类
 * @Author    紫梦
 * @DateTime  2017-11-10
 * @copyright 响响租车
 * @version   1.0
 * @UpdateTime 2017-11-10
 */
namespace app\index\controller;
use \think\Request;
use \think\Db;
use app\common\model\RentArt;
use app\common\model\Rent;
use app\common\model\Cars;
use app\common\model\Maintain;
use app\common\model\Inspection;
use app\common\model\Taxation;
use app\common\model\OtherMoney;
use app\common\model\Insure;
use app\common\model\Policy;
use app\common\model\Repair;
use app\common\request\Oss;
class Record extends Base
{
    /**
     * 维修记录管理
     */
    public function repair()
    {
        $model = Repair::All();
        $this->assign('data',$model);
        return $this->fetch();
    }

    /**
     * 删除维修记录
     */
    public function repairDel($id)
    {
        $msg['status'] = 0;
        $res = Repair::destroy($id);
        if(!$res) {$msg['status'] = 1; $msg['msg'] = '删除失败,请稍后再试';}
        
        return $msg;
    }
    /**
     * 取回维修车辆
     */
    public function repairRevert($id)
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
           
            $model = new Repair();
            
            try {
                $model->allowField(true)->isUpdate(true)->save($data);
            } catch (\Exception $e) {
                $msg['status'] = 1;
                $msg['msg'] = '数据未更新';
                return $msg;
            }

            $msg['msg'] = '更新成功';
            return $msg;
        }else{
            $model = Repair::get($id);          
            $this->assign('data',$model);
            return $this->fetch('repair_revert');
        }
    }

    /**
     * 编辑维修记录
     */
    public function repairEdit($id)
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
            unset($data['uploadfile']);

            $model = new Repair();
            try {
                $model->allowField(true)->isUpdate(true)->save($data);
            } catch (\Exception $e) {
                $msg['status'] = 1;
                $msg['msg'] = '数据未更新';
                return $msg;
            }

            $msg['msg'] = '更新成功';
            return $msg;

        }else{

            $model = Repair::get($id);
            $this->assign('data',$model);
            return $this->fetch('repair_edit');

        }
    }

    /**
     * 添加维修记录
     */
    public function repairAdd()
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
            $car = Cars::get(['car_sn'=>$data['car_sn']]);
            if(!$car){
                $msg['status'] = 1;
                $msg['msg'] = '车牌号不存在,请查正后再添加';
                return $msg;

            }
            $data['car_id'] = $car['car_id'];
            $data['car_sn'] = $car['car_sn'];
            $data['car_unique_id'] = $car['car_unique_id'];
            $data['repair_status'] = 1;
            $model = new Repair();
            
    
            $res = $model->allowField(true)->save($data);

            if($res){

                $msg['msg'] = '添加成功';
                
            }else{

                $msg['status'] = 1;
                $msg['msg'] = '添加失败,请稍后再试';
            }

            return $msg;
        }else{
            

            return $this->fetch('repair_add');

        }
    }

    /**
     * 投保管理 
     */
    public function insure()
    {
        $model = Insure::All();
        $this->assign('data',$model);
        // dump($model);
        return $this->fetch();
    }

    public function insureDel($id)
    {
        $msg['status'] = 0;
        $res = Insure::destroy($id);
        if(!$res) {$msg['status'] = 1; $msg['msg'] = '删除失败,请稍后再试';}
        
        return $msg;
    }

    /**
     * 删除投保电子文件
     */
    public function insureDelFile($id)
    {
        $msg['status'] = 0;
        $url = Db::name('insure_file')->where('id',$id)->find();
        $oss = new Oss();
        $res = false;
        if($oss->unlinkOss($url['file_url'])){            
            $res = Db::name('insure_file')->delete($id);
            
        }
        if(!$res) {$msg['status'] = 1; $msg['msg'] = '删除失败,请稍后再试';}
        
        return $msg;
    }

    /**
     * 编辑投保记录
     */
    public function insureEdit($id)
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
            //车辆ID
            $car = Cars::get(['car_unique_id'=>$data['car_unique_id']]);
            if(!$car){
                $msg['status'] = 1;
                $msg['msg'] = '车架号不存在,请查正后再添加';
                return $msg;

            }

            $data['car_id'] = $car['car_id'];
            $data['car_sn'] = $car['car_sn'];
            $model = new Insure();
            
            $res = $model->allowField(true)->isUpdate(true)->save($data);
            if($res){

                $msg['msg'] = '更新成功';
                
            }else{

                $msg['status'] = 1;
                $msg['msg'] = '数据未更新';
            }
            //电子文件
            $uploadfile = $request->file('uploadfile');
            if($uploadfile){
                $oss = new Oss();
                foreach ($uploadfile as $key => $value) {
                    $url = $oss->uploadOss($value);                 
                    if($url){
                        Db::name('insure_file')->insert(['insure_id'=>$data['id'],'file_url'=>$url]);
                    }
                }
            }

            return $msg;
        }else{
            $model = Insure::get($id);
            $this->assign('data',$model);
            return $this->fetch('insure_edit');

        }
    }

    /**
     * 添加投保记录
     */
    public function insureAdd()
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
            //车辆ID
            $car = Cars::get(['car_unique_id'=>$data['car_unique_id']]);
            if(!$car){
                $msg['status'] = 1;
                $msg['msg'] = '车架号不存在,请查正后再添加';
                return $msg;

            }
            $data['car_id'] = $car['car_id'];
            $data['car_sn'] = $car['car_sn'];
            $model = new Insure();
               
            $res = $model->allowField(true)->save($data);
            if($res){
                $msg['msg'] = '添加成功';
                
            }else{
                $msg['status'] = 1;
                $msg['msg'] = '添加失败,请稍后再试';
            }

            //电子文件
            $uploadfile = $request->file('uploadfile');
            if($uploadfile){
                $oss = new Oss();
                foreach ($uploadfile as $key => $value) {
                    $url = $oss->uploadOss($value);                 
                    if($url){
                        Db::name('insure_file')->insert(['insure_id'=>$model->id,'file_url'=>$url]);
                    }
                }
            }

            return $msg;
        }else{
            return $this->fetch('insure_add');

        }

    }
    
    /**
     * 保单管理
     */
    public function policy()
    {
        $model = Policy::All();
        $this->assign('data',$model);
        return $this->fetch();
    }

    /**
     * 删除保单
     */
    public function policyDel($id)
    {
        $msg['status'] = 0;

        $rent = Insure::get(['policy_id'=>$id]);
        if($rent) {
            $msg['status'] = 1; $msg['msg'] = '该文案正在使用,请勿删除';
            return $msg;
        }
        
        $res = Policy::destroy($id);
        if(!$res) {$msg['status'] = 1; $msg['msg'] = '删除失败,请稍后再试';}
        
        return $msg;
    }

    /**
     * 编辑保单
     */
    public function policyEdit($id)
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
            unset($data['uploadfile']);

            $model = new Policy();
            $file = $request->file('uploadfile');

            // 文件上传
            if($file)
            {
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    // 成功上传后 获取上传信息
                    $data['uploadfile'] = $info->getSaveName();
                }else{
                    // 上传失败获取错误信息
                    $msg['status'] = 1;
                    $msg['msg'] = $file->getError();

                    return $msg;
                }
            }
            $res = $model->allowField(true)->update($data);

            if($res){

                $msg['msg'] = '编辑成功';
                
            }else{

                $msg['status'] = 1;
                $msg['msg'] = '编辑失败,请稍后再试';
            }

            return $msg;
        }else{

            $model = Policy::get($id);
            $this->assign('data',$model);
            return $this->fetch('policy_edit');

        }
    }

    /**
     * 添加保单
     */
    public function policyAdd()
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();

            $model = new Policy();
            $file = $request->file('uploadfile');

                        // 文件上传
            if($file)
            {
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    // 成功上传后 获取上传信息
                    $data['uploadfile'] = $info->getSaveName();
                }else{
                    // 上传失败获取错误信息
                    $msg['status'] = 1;
                    $msg['msg'] = $file->getError();

                    return $msg;
                }
            }
        
            $res = $model->allowField(true)->save($data);

            if($res){

                $msg['msg'] = '添加成功';
                
            }else{

                $msg['status'] = 1;
                $msg['msg'] = '添加失败,请稍后再试';
            }

            return $msg;
        }else{

            return $this->fetch('policy_add');

        }
    }

    /**
     * 其他费用管理
     */
    public function otherMoney()
    {
        $model = OtherMoney::All();
        $this->assign('data',$model);
        return $this->fetch('other_money');
    }

    /**
     * 编辑其他费用
     */
    public function otherMoneyEdit($id)
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
            
            $model = new OtherMoney();
            unset($data['uploadfile']);
            // 文件上传
            $file = $request->file('uploadfile');

            if($file)
            {
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    // 成功上传后 获取上传信息
                    $data['uploadfile'] = $info->getSaveName();
                }else{
                    // 上传失败获取错误信息
                    $msg['status'] = 1;
                    $msg['msg'] = $file->getError();

                    return $msg;
                }
            }
            $res = $model->allowField(true)->update($data);

            if($res){

                $msg['msg'] = '编辑成功';
                
            }else{

                $msg['status'] = 1;
                $msg['msg'] = '编辑失败,请稍后再试';
            }

            return $msg;
        }else{

            $model = OtherMoney::get($id);
            
            
            $this->assign('data',$model);
            
            return $this->fetch('other_money_edit');

        }
    }

    /**
     * 添加其他费用
     */
    public function otherMoneyAdd()
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
            unset($data['uploadfile']);
            
            $model = new OtherMoney();
            $car = Cars::get(['car_sn'=>$data['car_sn']]);
            if(!$car){
                $msg['status'] = 1;
                $msg['msg'] = '车牌不存在,请查正后再添加';
                return $msg;

            }
            $data['car_id'] = $car['car_id'];
            // 文件上传
            $file = $request->file('uploadfile');

            if($file)
            {
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    // 成功上传后 获取上传信息
                    $data['uploadfile'] = $info->getSaveName();
                }else{
                    // 上传失败获取错误信息
                    $msg['status'] = 1;
                    $msg['msg'] = $file->getError();

                    return $msg;
                }
            }

            $res = $model->allowField(true)->save($data);
            // dump($model->inspection_time);
            if($res){

                $msg['msg'] = '添加成功';
                
            }else{

                $msg['status'] = 1;
                $msg['msg'] = '添加失败,请稍后再试';
            }

            return $msg;
        }else{

            
            return $this->fetch('other_money_add');

        }
    }

    /**
     * 税费列表
     */
    public function taxation()
    {
        $model = Taxation::All();
        $this->assign('data',$model);
        
        return $this->fetch();
    }

    /**
     * 删除税费记录
     */
    public function taxationDel($id)
    {
        $msg['status'] = 0;
        $res = Taxation::destroy($id);
        if(!$res) {$msg['status'] = 1; $msg['msg'] = '删除失败,请稍后再试';}
        
        return $msg;
    }

    /**
     * 税费记录编辑
     */
    public function taxationEdit($id)
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
            
            $model = new Taxation();
            unset($data['uploadfile']);
            // 文件上传
            $file = $request->file('uploadfile');

            if($file)
            {
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    // 成功上传后 获取上传信息
                    $data['uploadfile'] = $info->getSaveName();
                }else{
                    // 上传失败获取错误信息
                    $msg['status'] = 1;
                    $msg['msg'] = $file->getError();

                    return $msg;
                }
            }
            $res = $model->allowField(true)->update($data);

            if($res){

                $msg['msg'] = '编辑成功';
                
            }else{

                $msg['status'] = 1;
                $msg['msg'] = '编辑失败,请稍后再试';
            }

            return $msg;
        }else{

            $model = Taxation::get($id);
            
            
            $this->assign('data',$model);
            
            return $this->fetch('taxation_edit');

        }
    }

    /**
     * 税费记录添加
     */
    public function taxationAdd()
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
            unset($data['uploadfile']);
            
            $model = new Taxation();
            $car = Cars::get(['car_unique_id'=>$data['car_unique_id']]);
            if(!$car){
                $msg['status'] = 1;
                $msg['msg'] = '车架号不存在,请查正后再添加';
                return $msg;

            }
            $data['car_id'] = $car['car_id'];
            // 文件上传
            $file = $request->file('uploadfile');

            if($file)
            {
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    // 成功上传后 获取上传信息
                    $data['uploadfile'] = $info->getSaveName();
                }else{
                    // 上传失败获取错误信息
                    $msg['status'] = 1;
                    $msg['msg'] = $file->getError();

                    return $msg;
                }
            }

            $res = $model->allowField(true)->save($data);
            // dump($model->inspection_time);
            if($res){

                $msg['msg'] = '添加成功';
                
            }else{

                $msg['status'] = 1;
                $msg['msg'] = '添加失败,请稍后再试';
            }

            return $msg;
        }else{

            
            return $this->fetch('taxation_add');

        }
    }

    /**
     * 年检列表
     */
    public function inspection()
    {
        $model = Inspection::All();
        $this->assign('data',$model);

        return $this->fetch();
    }

    /**
     * 删除年检记录
     */
    public function inspectionDel()
    {
        $msg['status'] = 0;
        $res = Inspection::destroy($id);
        if(!$res) {$msg['status'] = 1; $msg['msg'] = '删除失败,请稍后再试';}
        
        return $msg;
    }

    /**
     * 编辑年检记录
     */
    public function inspectionEdit($id)
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
            
            $model = new Inspection();
            unset($data['uploadfile']);
            // 文件上传
            $file = $request->file('uploadfile');

            if($file)
            {
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    // 成功上传后 获取上传信息
                    $data['uploadfile'] = $info->getSaveName();
                }else{
                    // 上传失败获取错误信息
                    $msg['status'] = 1;
                    $msg['msg'] = $file->getError();

                    return $msg;
                }
            }
            $res = $model->allowField(true)->update($data);

            if($res){

                $msg['msg'] = '编辑成功';
                
            }else{

                $msg['status'] = 1;
                $msg['msg'] = '编辑失败,请稍后再试';
            }

            return $msg;
        }else{

            $model = Inspection::get($id);
            
            
            $this->assign('data',$model);
            
            return $this->fetch('inspection_edit');

        }
    }

    /**
     * 添加年检记录
     */
    public function inspectionAdd()
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
            unset($data['uploadfile']);
            
            $model = new Inspection();
            $car = Cars::get(['car_unique_id'=>$data['car_unique_id']]);
            if(!$car){
                $msg['status'] = 1;
                $msg['msg'] = '车架号不存在,请查正后再添加';
                return $msg;

            }
            $data['car_id'] = $car['car_id'];
            // 文件上传
            $file = $request->file('uploadfile');

            if($file)
            {
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    // 成功上传后 获取上传信息
                    $data['uploadfile'] = $info->getSaveName();
                }else{
                    // 上传失败获取错误信息
                    $msg['status'] = 1;
                    $msg['msg'] = $file->getError();

                    return $msg;
                }
            }

            $res = $model->allowField(true)->save($data);
            // dump($model->inspection_time);
            if($res){

                $msg['msg'] = '添加成功';
                
            }else{

                $msg['status'] = 1;
                $msg['msg'] = '添加失败,请稍后再试';
            }

            return $msg;
        }else{

            
            return $this->fetch('inspection_add');

        }
    }

    /**
     * 保养列表
     */
    public function maintain()
    {
        $model = Maintain::All();
        $this->assign('data',$model);
        return $this->fetch();
    }

    /**
     * 删除保养记录
     */
    public function maintainDel($id)
    {
        $msg['status'] = 0;
        $res = Maintain::destroy($id);
        if(!$res) {$msg['status'] = 1; $msg['msg'] = '删除失败,请稍后再试';}
        
        return $msg;
    }

    /**
     * 编辑保养记录
     */
    public function maintainEdit($id)
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
            
            $model = new Maintain();
            unset($data['uploadfile']);
            // 文件上传
            $file = $request->file('uploadfile');

            if($file)
            {
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    // 成功上传后 获取上传信息
                    $data['uploadfile'] = $info->getSaveName();
                }else{
                    // 上传失败获取错误信息
                    $msg['status'] = 1;
                    $msg['msg'] = $file->getError();

                    return $msg;
                }
            }
            $res = $model->allowField(true)->update($data);

            if($res){

                $msg['msg'] = '编辑成功';
                
            }else{

                $msg['status'] = 1;
                $msg['msg'] = '编辑失败,请稍后再试';
            }

            return $msg;
        }else{

            $model = Maintain::get($id);
            
            
            $this->assign('data',$model);
            
            return $this->fetch('maintain_edit');

        }

    }

    /**
     * 添加保养记录
     */
    public function maintainAdd()
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
            
            $model = new Maintain();
            $car = Cars::get(['car_sn'=>$data['car_sn']]);
            if(!$car){
                $msg['status'] = 1;
                $msg['msg'] = '车牌不存在,请查正后再添加';
                return $msg;

            }
            $data['car_id'] = $car['car_id'];
            unset($data['uploadfile']);
            // 文件上传
            $file = $request->file('uploadfile');

            if($file)
            {
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    // 成功上传后 获取上传信息
                    $data['uploadfile'] = $info->getSaveName();
                }else{
                    // 上传失败获取错误信息
                    $msg['status'] = 1;
                    $msg['msg'] = $file->getError();

                    return $msg;
                }
            }
    
            $res = $model->allowField(true)->save($data);

            if($res){

                $msg['msg'] = '添加成功';
                
            }else{

                $msg['status'] = 1;
                $msg['msg'] = '添加失败,请稍后再试';
            }

            return $msg;
        }else{

            
            return $this->fetch('maintain_add');

        }
    }
    
    /**
     * 租用列表
     */
    public function rent()
    {
        $request = Request::instance();
        $post = $request->post();
        
        if($request->isAjax()){
            $datemin = empty($post['datemin'])?'1970-1-1':$post['datemin'];
            $datemax = empty($post['datemax'])?time():$post['datemax'];

            

            $model= Rent::where('car_unique_id','like','%'.$post['search'].'%')
                            ->whereOr('rent','like','%'.$post['search'].'%')
                            ->where('rent_time','between time',[$datemin,$datemax])

                            ->order('id', 'desc')
                            ->paginate($post['length'],false,[
                                    'type'     => 'bootstrap',
                                    'var_page' => 'page',
                                    'page'=>$post['start']/$post['length'] +1 ,
                                ]);


            if(count($model)>=1){

                foreach ($model as $key => $value) {
                    $res['data'][] = $value;
                }
                
                $res['recordsTotal'] = $res['recordsFiltered'] = $model->total();
            }else{
                    $res['data']= '';
                    $res['recordsTotal'] = $res['recordsFiltered'] =0;
            }
            
            return $res;

        }else{
            
            $model = Rent::count();
            $this->assign('number',$model);

        // // dump($post);
            $this->assign('search_value',$post['search']??null);    
            $this->assign('datemin',$post['datemin']??null);    
            $this->assign('datemax',$post['datemax']??null);    
            return $this->fetch();
        }
    }

    /**
     * 删除租用记录
     */
    public function rentDel($id)
    {
        $msg['status'] = 0;
        $res = Rent::destroy($id);
        if(!$res) {$msg['status'] = 1; $msg['msg'] = '删除失败,请稍后再试';}
        
        return $msg;
    }

    /**
     * 删除租用记录附件
     */
    public function rentDelFile($id)
    {
        $msg['status'] = 0;
        $url = Db::name('rent_file')->where(['id'=>$id])->find();
        $oss = new Oss();
        $res = false;
        if($oss->unlinkOss($url['file_url'])){
            $res = Db::name('rent_file')->delete($id);
            
        }
        if(!$res) {$msg['status'] = 1; $msg['msg'] = '删除失败,请稍后再试';}
        
        return $msg;
    }

    /**
     * 租用车辆取回
     */
    public function rentRevert($id)
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
            $data['status'] = 2;
            $data['revert_time'] = time();
            $model = new Rent();
            
            try {
                $model->allowField(true)->isUpdate(true)->save($data);
            } catch (\Exception $e) {
                $msg['status'] = 1;
                $msg['msg'] = '数据未更新';
                return $msg;
            }
            
            $files = $request->file('uploadfile');
            if($files){
                $oss = new Oss();
                foreach ($files as $key => $value) {
                    $url = $oss->uploadOss($value);                    
                    if($url){
                        Db::name('rent_file')->insert(['rent_id'=>$data['id'],'file_url'=>$url]);
                    }
                }

            } 

            $msg['msg'] = '更新成功';
            return $msg;
        }else{
            $model = Rent::get($id);          
            $this->assign('data',$model);
            return $this->fetch('rent_revert');
        }
    }

    /**
     * 编辑租用记录
     */
    public function rentEdit($id)
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
            $car = Cars::get(['car_sn'=>$data['car_sn']]);
            
            if(empty($car)){
                $msg['status'] = 1;
                $msg['msg'] = '车牌号不存在,请查正后再修改';
                return $msg;

            }
            $data['car_id'] = $car['car_id'];
            $data['car_unique_id'] = $car['car_unique_id'];
            $model = new Rent();
            
            try {
                $model->allowField(true)->isUpdate(true)->save($data);
            } catch (\Exception $e) {
                $msg['status'] = 1;
                $msg['msg'] = '数据未更新';
                return $msg;
            }
           
            
            $files = $request->file('uploadfile');
            if($files){
                $oss = new Oss();
                foreach ($files as $key => $value) {
                    $url = $oss->uploadOss($value);                    
                    if($url){
                        Db::name('rent_file')->insert(['rent_id'=>$data['id'],'file_url'=>$url]);
                    }
                }

            } 

            $msg['msg'] = '更新成功';
            return $msg;
        }else{

            $model = Rent::get($id);          
            $this->assign('data',$model);
            return $this->fetch('rent_edit');

        }
    }

    /**
     * 添加租用记录
     */
    public function rentAdd()
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
            if($data){               
            $car = Cars::get(['car_sn'=>$data['car_sn']]);
            }
            if(empty($car)){
                $msg['status'] = 1;
                $msg['msg'] = '车牌号不存在,请查正后再添加';
                return $msg;

            }
            $data['car_id'] = $car['car_id'];
            $data['car_unique_id'] = $car['car_unique_id'];
            // if(!empty($data['type'])){
            //     Db::name('cars')->where(['car_id'=>$car['car_id']])->update(['relevance' => $data['type']]);
            // }
            
            $rent = new Rent($data);                 
            $rent->allowField(true)->save();

            $files = $request->file('uploadfile');
            if($files){
                $oss = new Oss();
                foreach ($files as $key => $value) {
                    $url = $oss->uploadOss($value);                    
                    if($url){
                        Db::name('rent_file')->insert(['rent_id'=>$rent->id,'file_url'=>$url]);
                    }
                }

            }   
            if($rent->id){

                $msg['msg'] = '添加成功';
                
            }else{

                $msg['status'] = 1;
                $msg['msg'] = '添加失败,请稍后再试';
            }
            return $msg;

        }else{
            return $this->fetch('rent_add');

        }
    }

    /**
     * 协议文案
     */
    public function rentArt()
    {
        $request = Request::instance();

        if($request->isAjax()){
            $model = RentArt::All();
            // return $model;
        }else{
            
            $model = RentArt::All();
            // dump($model);
            $this->assign('data',$model);
            return $this->fetch('rent_art');
        }
        
    }

    /**
     * 删除协议文案
     */
    public function rentArtDel($id)
    {
        $msg['status'] = 0;

        $rent = Rent::get(['rent_art'=>$id]);
        if($rent) {
            $msg['status'] = 1; $msg['msg'] = '该文案正在使用,请勿删除';
            return $msg;
        }

        $res = RentArt::destroy($id);
        if(!$res) {$msg['status'] = 1; $msg['msg'] = '删除失败,请稍后再试';}
        
        return $msg;
    }

    /**
     * 编辑协议文案
     */
    public function rentArtEdit($id)
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
            unset($data['uploadfile']);

            $model = new RentArt();
            $file = $request->file('uploadfile');

            // 文件上传
            if($file)
            {
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    // 成功上传后 获取上传信息
                    $data['uploadfile'] = $info->getSaveName();
                }else{
                    // 上传失败获取错误信息
                    $msg['status'] = 1;
                    $msg['msg'] = $file->getError();

                    return $msg;
                }
            }
            $res = $model->allowField(true)->update($data);

            if($res){

                $msg['msg'] = '编辑成功';
                
            }else{

                $msg['status'] = 1;
                $msg['msg'] = '编辑失败,请稍后再试';
            }

            return $msg;
        }else{

            $model = RentArt::get($id);
            $this->assign('data',$model);
            return $this->fetch('rent_art_edit');

        }
        
    }

    /**
     * 添加协议文案
     */
    public function rentArtAdd()
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();

            $model = new RentArt();
            $file = $request->file('uploadfile');

            // 文件上传
            if($file)
            {
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    // 成功上传后 获取上传信息
                    $data['uploadfile'] = $info->getSaveName();
                }else{
                    // 上传失败获取错误信息
                    $msg['status'] = 1;
                    $msg['msg'] = $file->getError();

                    return $msg;
                }
            }
        
            $res = $model->allowField(true)->save($data);

            if($res){

                $msg['msg'] = '添加成功';
                
            }else{

                $msg['status'] = 1;
                $msg['msg'] = '添加失败,请稍后再试';
            }

            return $msg;
        }else{

            return $this->fetch('rent_art_add');

        }
        
    }

    public function ajaxGetCarUid($query = 0)
    {
        $where = '%'.$query.'%';

        $data = Cars::where('car_unique_id','like',$where)->limit(10)->select();

        return $data;

    }

    public function ajaxGetCarSn($query = 0)
    {
        $where = '%'.$query.'%';

        $data = Cars::where('car_sn','like',$where)->limit(10)->select();

        return $data;
    }
    
}